home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume21 / rayshade / part02 < prev    next >
Encoding:
Internet Message Format  |  1990-03-21  |  56.4 KB

  1. Subject:  v21i009:  A ray tracing program, Part02/08
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: Craig Kolb <craig@weedeater.math.yale.edu>
  7. Posting-number: Volume 21, Issue 9
  8. Archive-name: rayshade/part02
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then unpack
  12. # it by saving it into a file and typing "sh file".  To overwrite existing
  13. # files, type "sh file -c".  You can also feed this as standard input via
  14. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  15. # will see the following message at the end:
  16. #        "End of archive 2 (of 8)."
  17. # Contents:  Examples/pool.ray nff2shade.awk src/datatypes.h
  18. #   src/funcdefs.h src/input.c src/list.c src/main.c src/outputp.c
  19. #   src/primobj.h src/setup.c src/sphere.c src/superq.c src/surface.c
  20. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  21. if test -f 'Examples/pool.ray' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'Examples/pool.ray'\"
  23. else
  24. echo shar: Extracting \"'Examples/pool.ray'\" \(3579 characters\)
  25. sed "s/^X//" >'Examples/pool.ray' <<'END_OF_FILE'
  26. X/*
  27. X * Example rayshade input file
  28. X *
  29. X * pool table from "Reds' Nightmare"
  30. X *
  31. X * Warning:  this will take a while to render, as it uses 9 rays/pixel
  32. X * and makes heavy use of texturing routines.
  33. X *
  34. X * To speed rendering, use -P or -S options.
  35. X *
  36. X * C. Kolb 2/88
  37. X */
  38. Xup 0.0 0.0 1.
  39. Xfov 45
  40. Xscreen 512 512
  41. Xmaxdepth 2
  42. Xbackground 0 0 0
  43. Xlight 1.4 point 20.0 0.0 66.0
  44. Xsurface blacktile 0.01 0.01 0.01  0.01 0.01 0.01 0. 0. 0. 0. 0. 0 0
  45. Xsurface s0  0.862745 0.862745 0.862745  0.039216 0.039216 0.039216  0. 0. 0.
  46. X        7. 0. 0. 1.
  47. Xsurface s1  0.470588 0.156863 0.392157  0.470588 0.156863 0.392157  0. 0. 0.
  48. X        25. 1. 0. 1.
  49. Xsurface s2  0.117647 0.117647 0.392157  0.117647 0.117647 0.392157  0. 0. 0.
  50. X        25. 0. 0. 1.
  51. Xsurface s3  0.784314 0.078431 0.078431  0.470588 0.039216 0.039216  0. 0. 0.
  52. X        25. 0. 0. 1.
  53. Xsurface s4 0 .2235 .145 0 .2235 .145 0 0 0 0.0 0. 0. 1.
  54. Xsurface mirror 0.04 0.04 0.04 0.05 0.05 0.05  1. 1. 1.  60. 0.95 0. 1.
  55. Xsurface s5  0.196078 0.392157 0.117647  0.196078 0.392157 0.117647
  56. X        0.156863 0.156863 0.156863  7. 0.1 0. 1.
  57. Xsurface s6  0.588235 0.392157 0.117647  0.196078 0.392157 0.117647
  58. X        0.156863 0.156863 0.156863  7. 0.1 0. 1.
  59. Xsurface s7  0.196078 0.392157 0.509804  0.196078 0.392157 0.117647
  60. X        0.156863 0.156863 0.156863  7. 0.1 0. 1.
  61. Xsurface s8  0.980392 0.196078 0.117647  0.196078 0.392157 0.117647
  62. X        0.156863 0.156863 0.156863  7. 0.1 0. 1.
  63. Xsurface s9  0.196078 0.392157 0.901961  0.196078 0. 0.117647
  64. X        0.156863 0.156863 0.156863  7. 0.1 0. 1.
  65. Xsurface s10  0.411765 0.411765 0.176471  0.411765 0.411765 0.176471  0. 0. 0.
  66. X        0. 0. 0. 1.
  67. Xsurface floor .1 .1 .1 .5 .5 .45 0.8 0.8 0.8 18 0.0 0 0
  68. X/*surface floor 0.5 0.5 0.5  0.8 0.78 0.78
  69. X        0.8 0.76 0.76  10. 0.0 0. 1. */
  70. Xsurface s12  0.313725 0.313725 0.313725  0.745098 0.745098 0.745098
  71. X        0. 0. 0.  0. 0. 0. 1.
  72. Xsurface s13  0.078431 0.862745 0.078431  0.039216 0.039216 0.039216
  73. X        0.156863 0.156863 0.156863  7. 0. 0. 1.
  74. Xsurface s14  0.784314 0.078431 0.078431  0.470588 0.039216 0.039216
  75. X        0. 0. 0.  25. 0. 0. 1.
  76. Xsurface s15  0.392157 0.098039 0.047059  0.392157 0.098039 0.047059
  77. X        0.039216 0.039216 0.039216  3. 0. 0. 1.
  78. Xsurface s16  0.509804 0.509804 0.509804  0.509804 0.509804 0.509804
  79. X        0.156863 0.156863 0.156863  7. 0.1 0. 1.
  80. X
  81. X    sphere s5 1.5  0.0    -21.        1.5
  82. X    sphere s6 1.5  1.5    -23.598026    1.5
  83. X    sphere s7 1.5 -1.5    -23.598026    1.5
  84. X    sphere s8 1.5  3.0    -26.196152    1.5
  85. X    sphere s9 1.5  0.0    -26.196152    1.5
  86. X    sphere s5 1.5 -3.0    -26.196152    1.5
  87. X    sphere s6 1.5  4.5    -28.794229    1.5
  88. X    sphere s7 1.5  1.5    -28.794229    1.5
  89. X    sphere s8 1.5 -1.5    -28.794229    1.5
  90. X    sphere s9 1.5 -4.5    -28.794229    1.5
  91. X    sphere s5 1.5  6.0    -31.392305    1.5
  92. X    sphere s6 1.5  3.0    -31.392305    1.5
  93. X    sphere s7 1.5  0.0    -31.392305    1.5
  94. X    sphere s8 1.5 -3.0    -31.392305    1.5
  95. X    sphere s9 1.5 -6.0    -31.392305    1.5
  96. X
  97. X    box s4 0. 0. -1. 30. 57. 1.
  98. X    box s15 30. 0. 0. 3. 54. 1.5 texture wood scale 5. 5. 5.
  99. X    box s15 -30. 0. 0. 3. 54. 1.5 texture wood scale 5. 5. 5.
  100. X    box s15 0. 57. 0. 33. 3. 1.5 texture wood scale 5. 5. 5.
  101. X    box s15 0. -57. 0. 33. 3. 1.5 texture wood scale 5. 5. 5.
  102. X    sphere s16 1.5 0. 0. 0. translate 0. 21. 1.5
  103. X    box mirror 10. -144. 30. 21.3333 .1 20.    /* was 40. -144 30. */
  104. X    /*
  105. X     * Walls
  106. X     */
  107. X    plane s10    0.     1.    0.     0.    -144.    0.
  108. X    plane s10    1.     0.    0.   -180.       0.    0.
  109. X    plane s10   -1.     0.    0.    180.       0.   0.
  110. X    plane s10    0.   -1.     0.    0.     144.    0.
  111. X    /*
  112. X     * Floor
  113. X     */
  114. X    plane floor   0.     0.    1.    0.       0. -30.
  115. X        texture marble scale 4. 4. 4. translate 0. 0. -4.376 
  116. X        texture checker blacktile scale 12.3 12.3 12.3
  117. X    /*
  118. X     * Ceiling
  119. X     */
  120. X    plane s12    0.   0.    -1.      0.       0.  72.
  121. X
  122. Xeyep 38. 100. 43.
  123. Xlookp 0. 0. 0.
  124. Xgrid 20 20 10
  125. Xjittered
  126. Xsamples 3
  127. END_OF_FILE
  128. if test 3579 -ne `wc -c <'Examples/pool.ray'`; then
  129.     echo shar: \"'Examples/pool.ray'\" unpacked with wrong size!
  130. fi
  131. # end of 'Examples/pool.ray'
  132. fi
  133. if test -f 'nff2shade.awk' -a "${1}" != "-c" ; then 
  134.   echo shar: Will not clobber existing file \"'nff2shade.awk'\"
  135. else
  136. echo shar: Extracting \"'nff2shade.awk'\" \(3223 characters\)
  137. sed "s/^X//" >'nff2shade.awk' <<'END_OF_FILE'
  138. X#
  139. X# This awk script will convert an NFF-format input file (as output by
  140. X# Eric Haines' SPD) to something rayshade can understand.
  141. X# The World object will be enclosed in a single grid of 22*22*22 voxels.
  142. X#
  143. X# Example usage:
  144. X#    mountains | awk -f nff2shade.awk | rayshade > mountains.rle
  145. X#
  146. X# For best results, one should modify the output for all but the tetra
  147. X# and mountain databases to provide a tighter bounding box around the
  148. X# object of interest (tree, gears, etc.).  This is done by placing
  149. X# all primitives but the ground pologon into an object stored as a Grid,
  150. X# and then instantiating that object once.  This will decrease ray-tracing
  151. X# time dramatically.
  152. X#
  153. X# $Id: nff2shade.awk,v 3.0 89/10/27 01:30:24 craig Exp $
  154. X#
  155. X# $Log:    nff2shade.awk,v $
  156. X# Revision 3.0  89/10/27  01:30:24  craig
  157. X# Baseline for first official release.
  158. X# 
  159. X# C. Kolb 9/21/89
  160. X# Fixed comment bug; check first character rather than first field for "#".
  161. X# Keep track of defined surfaces in associative array so if one is used
  162. X# again we don't create another copy.
  163. X# Halved ambient intensity of surfaces.
  164. X# C. Kolb 10/12/89
  165. X# Added "adaptive 0" and "cutoff 0" to BEGIN and removed "endfile" from END.
  166. X# C. Kolb 2/89
  167. X# First version.
  168. X#
  169. XBEGIN{
  170. X    lights = 0;
  171. X    surfs = 0;
  172. X    print "maxdepth 4"
  173. X    print "adaptive 0"
  174. X    print "cutoff 0."
  175. X}
  176. Xsubstr($1, 1, 1) == "#" { print "/* " $0 " */"; next;}
  177. X$1 == "v" { next;}
  178. X$1 == "from" { print "eyep " $2 " "$3 " " $4; next;}
  179. X$1 == "at" {print "lookp " $2 " "$3 " "$4; next;}
  180. X$1 == "up" {print; next;}
  181. X$1 == "angle" { print "fov " $2; next;}
  182. X$1 == "hither" {next;}
  183. X$1 == "resolution" {print "screen " $2 " "$3; next;}
  184. X
  185. X$1 == "l" { lightd[lights] = $2 " "$3 " "$4; lights++; next; }
  186. X$1 == "b" {print "background " $2 " "$3 " "$4; next; }
  187. X$1 == "f" {
  188. X    if (surfaces[$2 $3 $4 $5 $6 $7 $8] != 0) {
  189. X        cursurf = surfaces[$2 $3 $4 $5 $6 $7 $8];
  190. X        next;
  191. X    }
  192. X    surfs++;
  193. X    surfaces[$2 $3 $4 $5 $6 $7 $8] = surfs;
  194. X    cursurf = surfs;
  195. X    aintens = sqrt(lights) / (4*lights);
  196. X    dr = $2*$5;
  197. X    dg = $3*$5;
  198. X    db = $4*$5;
  199. X# this is a good guess....
  200. X    ar = aintens*dr;
  201. X    ag = aintens*dg;
  202. X    ab = aintens*db;
  203. X#
  204. X# We set "reflectance" to the minimum of Ks and 1. - T, as
  205. X# Eric Haines' SPD includes several objects which have Ks + T > 1.
  206. X#
  207. X    if ($6 < 1. - $8)
  208. X        reflect = $6;
  209. X    else
  210. X        reflect = 1. - $8;
  211. X    printf("surface s%d  %f %f %f  %f %f %f  %f %f %f  %f %f %f %f\n", \
  212. X        cursurf, ar, ag, ab, dr, dg, db, $6, $6, $6, $7, reflect, $8, $9);
  213. X    next;
  214. X}
  215. X
  216. X$1 == "c" {
  217. X    getline;
  218. X    x1 = $1;
  219. X    y1 = $2;
  220. X    z1 = $3;
  221. X    br = $4;
  222. X    getline;
  223. X    printf("cone s%d %f %f %f %f %f %f %f %f\n", \
  224. X        cursurf, x1, y1, z1, $1, $2, $3, br, $4);
  225. X    next;
  226. X}
  227. X$1 == "s" {
  228. X    print "sphere s"cursurf " "$5 " "$2 " "$3 " "$4;
  229. X    next;
  230. X}
  231. X$1 == "pp" {
  232. X    if ($2 == 3)
  233. X        print "triangle s"cursurf;
  234. X    else
  235. X        print "poly s"cursurf;
  236. X    next;
  237. X}
  238. X$1 == "p" {
  239. X#
  240. X# Polygon -- the vertices will print out in the default statement.
  241. X# If there are three vertices, make it a triangle.
  242. X#
  243. X    if ($2 == 3)
  244. X        print "triangle s"cursurf;
  245. X    else
  246. X        print "poly s"cursurf;
  247. X    next;
  248. X}
  249. X{
  250. X# Matched nothing (or is a vertex data) -- print it.
  251. X    print;
  252. X    next;
  253. X}
  254. XEND{
  255. X#
  256. X# Output light definitions.
  257. X#
  258. X    intens = sqrt(lights) / (lights);
  259. X    for (i = 0; i < lights; i++) {
  260. X        print "light " intens " point " lightd[i]
  261. X    }
  262. X    print "grid 22 22 22"
  263. X}
  264. END_OF_FILE
  265. if test 3223 -ne `wc -c <'nff2shade.awk'`; then
  266.     echo shar: \"'nff2shade.awk'\" unpacked with wrong size!
  267. fi
  268. # end of 'nff2shade.awk'
  269. fi
  270. if test -f 'src/datatypes.h' -a "${1}" != "-c" ; then 
  271.   echo shar: Will not clobber existing file \"'src/datatypes.h'\"
  272. else
  273. echo shar: Extracting \"'src/datatypes.h'\" \(3914 characters\)
  274. sed "s/^X//" >'src/datatypes.h' <<'END_OF_FILE'
  275. X/*
  276. X * datatypes.h
  277. X *
  278. X * Copyright (C) 1989, Craig E. Kolb
  279. X *
  280. X * This software may be freely copied, modified, and redistributed,
  281. X * provided that this copyright notice is preserved on all copies.
  282. X *
  283. X * There is no warranty or other guarantee of fitness for this software,
  284. X * it is provided solely .  Bug reports or fixes may be sent
  285. X * to the author, who may or may not act on them as he desires.
  286. X *
  287. X * You may not include this software in a program or other software product
  288. X * without supplying the source, or without informing the end-user that the
  289. X * source is available for no extra charge.
  290. X *
  291. X * If you modify this software, you should include a notice giving the
  292. X * name of the person performing the modification, the date of modification,
  293. X * and the reason for such modification.
  294. X *
  295. X * $Id: datatypes.h,v 3.0 89/10/27 02:05:49 craig Exp $
  296. X *
  297. X * $Log:    datatypes.h,v $
  298. X * Revision 3.0  89/10/27  02:05:49  craig
  299. X * Baseline for first official release.
  300. X * 
  301. X */
  302. X
  303. X#ifdef NOVOID
  304. Xtypedef int void;
  305. X#endif
  306. X
  307. Xtypedef struct {
  308. X    double u, v;            /* 2D point */
  309. X} Vec2d;
  310. X
  311. Xtypedef struct Vector {
  312. X    double x, y, z;            /* 3D point */
  313. X} Vector;
  314. X
  315. Xtypedef struct Ray {
  316. X    Vector pos;            /* Origin */
  317. X    Vector     dir;            /* Direction */
  318. X    char shadow;
  319. X    struct SurfaceList *media;    /* Medium ray is passing through */
  320. X} Ray;
  321. X
  322. Xtypedef struct Color {
  323. X    double r, g, b;            /* Red, green, blue. */
  324. X} Color;
  325. X
  326. Xtypedef struct {
  327. X    double matrix[3][3];        /* Rotation matrix */
  328. X    Vector translate;        /* Translation */
  329. X} TransInfo;
  330. X
  331. Xtypedef struct Trans {
  332. X    TransInfo world2obj,    /* worldspace --> object space */
  333. X          obj2world;    /* object space --> world space */
  334. X} Trans;
  335. X
  336. Xtypedef struct {
  337. X    struct ObjList *list;        /* List of prims/objs. in object */
  338. X    struct ObjList *unbounded;    /* List of unbounded prims. */
  339. X    double bounds[2][3];        /* Bounding box of object */
  340. X} List;
  341. X
  342. Xtypedef struct {
  343. X    short xsize, ysize, zsize;    /* # of voxels along each axis */
  344. X    double bounds[2][3];        /* bounding box */
  345. X    double voxsize[3];        /* size of a voxel */
  346. X    struct ObjList ****cells;    /* Voxels */
  347. X    struct ObjList *unbounded;    /* Unbounded objects */
  348. X} Grid;
  349. X
  350. X/*
  351. X * Surface definition.
  352. X */
  353. Xtypedef struct Surface {
  354. X    char *name;            /* Name */
  355. X    struct Color amb;        /* Ambient color */
  356. X    struct Color diff;        /* Diffuse color */
  357. X    struct Color spec;        /* Specular color */
  358. X    double coef;            /* Phong shading coef. */
  359. X    double refl;            /* Reflectivity (0-1) */
  360. X    double transp;            /* Transparency (0-1) */
  361. X    double kref;            /* Index of refraction */
  362. X    double translucency;        /* translucency (0-1) */
  363. X    double stcoef;            /* Phong coef. for transmitted light */
  364. X} Surface;
  365. X
  366. Xtypedef struct SurfaceList {
  367. X    Surface *surf;
  368. X    struct SurfaceList *next;
  369. X} SurfaceList;
  370. X
  371. Xtypedef struct ObjList {
  372. X    struct Object *data;        /* Pointer to object data */
  373. X    struct ObjList *next;        /* Next in list */
  374. X} ObjList;
  375. X
  376. Xtypedef struct PointList {
  377. X    Vector vec;            /* Vector data */
  378. X    struct PointList *next;        /* Next in list */
  379. X} PointList;
  380. X
  381. X/*
  382. X * Data about point of intersection.
  383. X * (May be modified by texture mapping.)
  384. X */
  385. Xtypedef struct HitInfo {
  386. X    Vector    pos,        /* Location of intersection */
  387. X        norm;        /* Normal to surface at int. point */
  388. X    struct Primitive *prim; /* Pointer to primitive hit. */
  389. X    struct Surface surf;    /* Surface to be used. */
  390. X    TransInfo *totaltrans;
  391. X} HitInfo;
  392. X
  393. X/*
  394. X * General-purpose texture structure.  The (bad) idea is to
  395. X * have one structure which every texturing function will use.
  396. X */
  397. Xtypedef struct Texture {
  398. X    char type;        /* Texture type */
  399. X    Surface *surf1;        /* Alternate surface */
  400. X    double size;        /* Scale/size factor */
  401. X    double *args;        /* Random arguments. */
  402. X    Color *colormap;    /* Colormap */
  403. X    Trans *trans;        /* Transformation matrices. */
  404. X    struct Texture *next;    /* Pointer to next texture. */
  405. X} Texture;
  406. X
  407. Xtypedef struct {
  408. X    Color color;
  409. X    double trans;
  410. X} Fog;
  411. X
  412. Xtypedef struct {
  413. X    Color color;        /* Mist color */
  414. X    Color trans;        /* R, G, B trans. */
  415. X    double scale, zero;    /* Height scale, start Z */
  416. X} Mist;
  417. END_OF_FILE
  418. if test 3914 -ne `wc -c <'src/datatypes.h'`; then
  419.     echo shar: \"'src/datatypes.h'\" unpacked with wrong size!
  420. fi
  421. # end of 'src/datatypes.h'
  422. fi
  423. if test -f 'src/funcdefs.h' -a "${1}" != "-c" ; then 
  424.   echo shar: Will not clobber existing file \"'src/funcdefs.h'\"
  425. else
  426. echo shar: Extracting \"'src/funcdefs.h'\" \(4762 characters\)
  427. sed "s/^X//" >'src/funcdefs.h' <<'END_OF_FILE'
  428. X/*
  429. X * funcdefs.h
  430. X *
  431. X * Copyright (C) 1989, Craig E. Kolb
  432. X *
  433. X * This software may be freely copied, modified, and redistributed,
  434. X * provided that this copyright notice is preserved on all copies.
  435. X *
  436. X * There is no warranty or other guarantee of fitness for this software,
  437. X * it is provided solely .  Bug reports or fixes may be sent
  438. X * to the author, who may or may not act on them as he desires.
  439. X *
  440. X * You may not include this software in a program or other software product
  441. X * without supplying the source, or without informing the end-user that the
  442. X * source is available for no extra charge.
  443. X *
  444. X * If you modify this software, you should include a notice giving the
  445. X * name of the person performing the modification, the date of modification,
  446. X * and the reason for such modification.
  447. X *
  448. X * $Id: funcdefs.h,v 3.0 89/10/27 02:05:50 craig Exp $
  449. X *
  450. X * $Log:    funcdefs.h,v $
  451. X * Revision 3.0  89/10/27  02:05:50  craig
  452. X * Baseline for first official release.
  453. X * 
  454. X */
  455. X
  456. X/*
  457. X * This file should eventually disappear; function declarations should
  458. X * appear as externs where needed.  Macros should be moved to a separate file.
  459. X */
  460. X/*
  461. X * Normal routines
  462. X */
  463. Xint    nrmsph(), nrmbox(), nrmtri(), nrmsup(),nrmplane(), nrmcyl(),
  464. X        nrmpoly(), nrmcone(), nrmhf();
  465. X/*
  466. X * Intersection routines
  467. X */
  468. Xdouble     intsph(), intbox(), inttri(), intsup(),intplane(), crossp(), intcyl(),
  469. X        intpoly(), intcone(), inthf();
  470. X/*
  471. X * Extent-box finding routines
  472. X */
  473. Xint    sphextent(),boxextent(),triextent(),supextent(),planeextent(),
  474. X        cylextent(), polyextent(), coneextent(), hfextent();
  475. X
  476. X/*
  477. X * Object creation routines
  478. X */
  479. XObject *maksph(), *makbox(), *maktri(), *maksup(), *makplane(), *makcyl(),
  480. X      *makpoly(), *makcone(), *makhf(), *new_object();
  481. X/*
  482. X * Intersection routines.
  483. X */
  484. Xdouble    int_grid(), int_list(), int_primitive(), IntBounds();
  485. X
  486. X/*
  487. X * Misc.
  488. X */
  489. Xchar *Malloc(), *Calloc(), *strsave();
  490. Xdouble    normalize(), Noise();
  491. X
  492. X/*
  493. X * Transformations
  494. X */
  495. XTrans *new_trans();
  496. XTransInfo *new_transinfo();
  497. X
  498. X/*
  499. X * Surfaces
  500. X */
  501. XSurface    *find_surface(), *make_surface(), *get_surface();
  502. XSurfaceList *add_surface();
  503. X/*
  504. X * Objects
  505. X */
  506. XObject *add_child_named(), *add_child(), *get_object_named();
  507. X
  508. Xextern double ftmp;  /* Yick -- keeps us from evaluating twice during fabs. */
  509. X
  510. X/*
  511. X * Macros
  512. X */
  513. X
  514. X/*
  515. X * Absolute value -- uses "ftmp" to store value of argument, which
  516. X * keeps us from evaluating expressions more than once.
  517. X */
  518. X#define    fabs(x)            ((ftmp=(x)) < 0. ? -(ftmp) : (ftmp))
  519. X#define abs(x)             ((x) < 0 ? -(x) : (x))
  520. X
  521. X#ifdef SYSV
  522. Xdouble drand48();
  523. X/*
  524. X * nrand() returns a uniformly distributed random variable between 0 and 1.
  525. X */
  526. X#define nrand()            (drand48())
  527. X#else
  528. Xlong random();
  529. X#define nrand()            ((double)random() / (double)((1 << 31) - 1))
  530. X#endif
  531. X
  532. X#ifdef MULTIMAX
  533. X/*
  534. X * On the multimax, allocate large pieces of memory as shared memory.
  535. X */
  536. Xextern char *share_malloc(), *share_calloc();
  537. X#define mallocprim()        (Primitive *)share_malloc(sizeof(Primitive))
  538. X#else
  539. X/*
  540. X * Otherwise, malloc is malloc, etc.
  541. X */
  542. X#define share_malloc(x)        Malloc(x)
  543. X#define share_calloc(x,y)    Calloc(x,y)
  544. X#define mallocprim()        (Primitive *)Malloc(sizeof(Primitive))
  545. X#endif
  546. X
  547. X/*
  548. X * Return a uniformly distributed random variable between -s/2 and s/2.
  549. X */
  550. X#define jitter(s)        ((nrand() * (s)) - (s)/2.)
  551. X/*
  552. X * Dot product
  553. X */
  554. X#define dotp(a, b)        (((a)->x*(b)->x)+((a)->y*(b)->y)+((a)->z*(b)->z))
  555. X/*
  556. X * Close enough for us.
  557. X */
  558. X#define equal(a, b)        (fabs((a) - (b)) < EPSILON)
  559. X/*
  560. X * Maximum/Minimum functions
  561. X */
  562. X#define max(a, b)        ((a) > (b) ? (a) : (b))
  563. X#define min(a, b)        ((a) < (b) ? (a) : (b))
  564. X/*
  565. X * Convert from voxel number along X/Y/Z to corresponding coordinate.
  566. X */
  567. X#define voxel2x(g,x)        ((x) * g->voxsize[0]+ g->bounds[0][0])
  568. X#define voxel2y(g,y)        ((y) * g->voxsize[1] + g->bounds[0][1])
  569. X#define voxel2z(g,z)        ((z) * g->voxsize[2] + g->bounds[0][2])
  570. X/*
  571. X * And vice-versa.
  572. X */
  573. X#define x2voxel(g,x)        (((x) - g->bounds[0][0]) / g->voxsize[0])
  574. X#define y2voxel(g,y)        (((y) - g->bounds[0][1]) / g->voxsize[1])
  575. X#define z2voxel(g,z)        (((z) - g->bounds[0][2]) / g->voxsize[2])
  576. X/*
  577. X * Is the point "p" outisde of the bounding box "b"?
  578. X */
  579. X#define OutOfBounds(p,b) ((p)->x < b[0][0] || (p)->x > b[1][0] ||\
  580. X              (p)->y < b[0][1] || (p)->y > b[1][1] ||\
  581. X              (p)->z < b[0][2] || (p)->z > b[1][2])
  582. X
  583. X#ifndef DUMB_CPP
  584. X
  585. X#define vecsub(a,b,r)    (r)->x= (a).x-(b).x;(r)->y= (a).y-(b).y;(r)->z= (a).z-(b).z
  586. X#define vecadd(a,b,r)    (r)->x= (a).x+(b).x;(r)->y= (a).y+(b).y;(r)->z= (a).z+(b).z
  587. X#define scalar_prod(s,a,r)    (r)->x= s*(a).x;(r)->y= s*(a).y;(r)->z= s*(a).z
  588. X#define veccomb(s1,v1,s2,v2,r)    (r)->x = s1*(v1).x + s2*(v2).x; \
  589. X                 (r)->y = s1*(v1).y + s2*(v2).y; \
  590. X                 (r)->z = s1*(v1).z + s2*(v2).z;
  591. X#define addscaledvec(v1,s,v2,r)    (r)->x = (v1).x + s*(v2).x; \
  592. X                 (r)->y = (v1).y + s*(v2).y; \
  593. X                 (r)->z = (v1).z + s*(v2).z;
  594. X#endif
  595. END_OF_FILE
  596. if test 4762 -ne `wc -c <'src/funcdefs.h'`; then
  597.     echo shar: \"'src/funcdefs.h'\" unpacked with wrong size!
  598. fi
  599. # end of 'src/funcdefs.h'
  600. fi
  601. if test -f 'src/input.c' -a "${1}" != "-c" ; then 
  602.   echo shar: Will not clobber existing file \"'src/input.c'\"
  603. else
  604. echo shar: Extracting \"'src/input.c'\" \(3342 characters\)
  605. sed "s/^X//" >'src/input.c' <<'END_OF_FILE'
  606. X/*
  607. X * input.c
  608. X *
  609. X * Copyright (C) 1989, Craig E. Kolb
  610. X *
  611. X * This software may be freely copied, modified, and redistributed,
  612. X * provided that this copyright notice is preserved on all copies.
  613. X *
  614. X * There is no warranty or other guarantee of fitness for this software,
  615. X * it is provided solely .  Bug reports or fixes may be sent
  616. X * to the author, who may or may not act on them as he desires.
  617. X *
  618. X * You may not include this software in a program or other software product
  619. X * without supplying the source, or without informing the end-user that the
  620. X * source is available for no extra charge.
  621. X *
  622. X * If you modify this software, you should include a notice giving the
  623. X * name of the person performing the modification, the date of modification,
  624. X * and the reason for such modification.
  625. X *
  626. X * $Id: input.c,v 3.0 89/10/27 02:05:51 craig Exp $
  627. X *
  628. X * $Log:    input.c,v $
  629. X * Revision 3.0  89/10/27  02:05:51  craig
  630. X * Baseline for first official release.
  631. X * 
  632. X */
  633. X#include <stdio.h>
  634. X#ifdef SYSV
  635. X#include <string.h>
  636. X#else
  637. X#ifndef AZTEC_C
  638. X#include <strings.h>
  639. X#else /* AZTEC_C */
  640. X
  641. Xgetpid()
  642. X{
  643. X    return 123;
  644. X}
  645. X#endif
  646. X#endif
  647. X#include "constants.h"
  648. X#include "typedefs.h"
  649. X
  650. X#define INCLUDE_STR    "#include "
  651. X
  652. Xchar *infilename;    /* Name of input file.  NULL signifies stdin. */
  653. Xchar tmpname[BUFSIZ];    /* name of temporary file */
  654. Xextern FILE *yyin;    /* lex/yacc file pointer */
  655. X
  656. Xread_input_file()
  657. X{
  658. X    extern char *infilename;
  659. X
  660. X    /*
  661. X     * Open temporary file.
  662. X     */
  663. X    sprintf(tmpname,"%s/raytmp.%d",TMPDIR, getpid());
  664. X    yyin = fopen(tmpname, "w");
  665. X
  666. X    if (yyin == (FILE *)NULL) {
  667. X        fprintf(stderr,"Cannot write to temp file %s\n",tmpname);
  668. X        exit(1);
  669. X    }
  670. X
  671. X    if (!process_file(infilename)) {
  672. X        /*
  673. X         * Some kind of error occurred -- unlink
  674. X         * temporary file and exit.
  675. X         */
  676. X        fclose(yyin);
  677. X        unlink(tmpname);
  678. X        exit(1);
  679. X    }
  680. X    /*
  681. X     * File processed okay.  Close the file, open it again for
  682. X     * reading, and call lex/yacc.
  683. X     */
  684. X    fclose(yyin);
  685. X    yyin = fopen(tmpname, "r");
  686. X    yyparse();
  687. X    /*
  688. X     * All done -- unlink temporary file.
  689. X     */
  690. X    unlink(tmpname);
  691. X}
  692. X
  693. X/*
  694. X * Open the named file and copy its contents into the temporary file.
  695. X * If we run across a #include directive, recurse on the desired
  696. X * file.  Note that *no checking is performed* -- a file could,
  697. X * for example, include itself.  Rayshade will happily go on
  698. X * copying data into the temporary file until it runs out
  699. X * of file pointers (an fopen will fail), or you run out of disk space...
  700. X */
  701. Xprocess_file(filename)
  702. Xchar *filename;
  703. X{
  704. X    FILE *fp;
  705. X    char buf[BUFSIZ], *name, *np;
  706. X    extern int yylineno;
  707. X
  708. X    if (filename == (char *)NULL)
  709. X        fp = stdin;
  710. X    else {
  711. X        fp = fopen(filename, "r");
  712. X        if (fp == (FILE *)NULL) {
  713. X            fprintf(stderr,"Cannot open %s for reading.\n",filename);
  714. X            return FALSE;
  715. X        }
  716. X    }
  717. X
  718. X    while(fgets(buf, BUFSIZ, fp) != NULL) {
  719. X        if (strncmp(buf, INCLUDE_STR, strlen(INCLUDE_STR)) == 0) {
  720. X            /*
  721. X             * Got an "#include "...
  722. X             */
  723. X#ifdef SYSV
  724. X            name = strchr(buf, '"');
  725. X            np = strrchr(buf, '"');
  726. X#else
  727. X            name = index(buf, '"');
  728. X            np = rindex(buf, '"');
  729. X#endif
  730. X            /*
  731. X             * Get name between quotes.  If no or
  732. X             * one quote, complain.
  733. X             */
  734. X            if (name == (char *)0 || name == np) {
  735. X                fprintf(stderr,"Invalid include directive (line %d)\n", yylineno);
  736. X                return FALSE;
  737. X            }
  738. X            name++;
  739. X            *np = (char)NULL;
  740. X            if (process_file(name) == FALSE)
  741. X                return FALSE;
  742. X        } else
  743. X            fputs(buf, yyin);
  744. X    }
  745. X    return TRUE;
  746. X}
  747. END_OF_FILE
  748. if test 3342 -ne `wc -c <'src/input.c'`; then
  749.     echo shar: \"'src/input.c'\" unpacked with wrong size!
  750. fi
  751. # end of 'src/input.c'
  752. fi
  753. if test -f 'src/list.c' -a "${1}" != "-c" ; then 
  754.   echo shar: Will not clobber existing file \"'src/list.c'\"
  755. else
  756. echo shar: Extracting \"'src/list.c'\" \(3185 characters\)
  757. sed "s/^X//" >'src/list.c' <<'END_OF_FILE'
  758. X/*
  759. X * list.c
  760. X *
  761. X * Copyright (C) 1989, Craig E. Kolb
  762. X *
  763. X * This software may be freely copied, modified, and redistributed,
  764. X * provided that this copyright notice is preserved on all copies.
  765. X *
  766. X * There is no warranty or other guarantee of fitness for this software,
  767. X * it is provided solely .  Bug reports or fixes may be sent
  768. X * to the author, who may or may not act on them as he desires.
  769. X *
  770. X * You may not include this software in a program or other software product
  771. X * without supplying the source, or without informing the end-user that the
  772. X * source is available for no extra charge.
  773. X *
  774. X * If you modify this software, you should include a notice giving the
  775. X * name of the person performing the modification, the date of modification,
  776. X * and the reason for such modification.
  777. X *
  778. X * $Id: list.c,v 3.0 89/10/27 02:05:54 craig Exp $
  779. X *
  780. X * $Log:    list.c,v $
  781. X * Revision 3.0  89/10/27  02:05:54  craig
  782. X * Baseline for first official release.
  783. X * 
  784. X */
  785. X#include <stdio.h>
  786. X#include "constants.h"
  787. X#include "typedefs.h"
  788. X#include "funcdefs.h"
  789. X/*
  790. X * Take a list whose DATA field points to a linked list of objects and
  791. X * turn it into a List.
  792. X */
  793. Xmake_list(obj)
  794. XObject *obj;
  795. X{
  796. X    int i;
  797. X    List *list;
  798. X    extern ObjList *find_bounds();
  799. X
  800. X    list = (List *)Malloc(sizeof(List));
  801. X    /*
  802. X     * Find the unbounded objects on the list as well as the
  803. X     * bounding box of the list.
  804. X     */
  805. X    list->unbounded = find_bounds((ObjList **)&obj->data, obj->bounds);
  806. X    /*
  807. X     * Transform bounding box if necessary.
  808. X     */
  809. X    if (obj->trans)
  810. X        transform_bounds(&obj->trans->obj2world, obj->bounds);
  811. X    for (i = 0; i < 3; i++) {
  812. X        list->bounds[LOW][i] = obj->bounds[LOW][i];
  813. X        list->bounds[HIGH][i] = obj->bounds[HIGH][i];
  814. X    }
  815. X    /*
  816. X     * obj->data now holds list of bounded objects.
  817. X     */
  818. X    list->list = (ObjList *)obj->data;
  819. X    obj->data = (char *)list;
  820. X}
  821. X
  822. X/*
  823. X * Intersect ray & list of objects.
  824. X */
  825. Xdouble
  826. Xint_list(list, source, ray, hitinfo)
  827. XList *list;
  828. XPrimitive *source;
  829. XRay *ray;
  830. XHitInfo *hitinfo;
  831. X{
  832. X    register ObjList *objlist;
  833. X    double offset;
  834. X    HitInfo hittmp;
  835. X    double dist, mindist;
  836. X    extern double intersect();
  837. X
  838. X    mindist = FAR_AWAY;
  839. X    hittmp.totaltrans = hitinfo->totaltrans;
  840. X    /*
  841. X     * Intersect with unbounded objects.
  842. X     */
  843. X    for (objlist = list->unbounded; objlist ; objlist = objlist->next) {
  844. X        dist = intersect((Object *)objlist->data, source, ray,
  845. X                    &hittmp);
  846. X        if (dist > EPSILON && dist < mindist) {
  847. X            mindist = dist;
  848. X            *hitinfo = hittmp;
  849. X        }
  850. X    }
  851. X    /*
  852. X     * Check for intersection with bounding box.
  853. X     */
  854. X    if (OutOfBounds(&ray->pos, list->bounds)) {
  855. X        offset = IntBounds(ray, list->bounds);
  856. X        if (offset < EPSILON)
  857. X            /*
  858. X             * Ray never hit list.
  859. X             */
  860. X            return (mindist == FAR_AWAY ? 0. : mindist);
  861. X        else if (mindist < offset)
  862. X            /*
  863. X             * Ray hit unbounded object closer than bounding box.
  864. X             */
  865. X            return mindist;
  866. X        /*
  867. X         * Else the ray enters list-space before it hits an
  868. X         * unbounded object.
  869. X         */
  870. X    }
  871. X    /*
  872. X     * Intersect with objects on list.
  873. X     */
  874. X    for (objlist = list->list; objlist ; objlist = objlist->next) {
  875. X        dist = intersect((Object *)objlist->data, source, ray,
  876. X                        &hittmp);
  877. X        if (dist > EPSILON && dist < mindist) {
  878. X            mindist = dist;
  879. X            *hitinfo = hittmp;
  880. X        }
  881. X    }
  882. X
  883. X    return (mindist == FAR_AWAY ? 0. : mindist);
  884. X}
  885. END_OF_FILE
  886. if test 3185 -ne `wc -c <'src/list.c'`; then
  887.     echo shar: \"'src/list.c'\" unpacked with wrong size!
  888. fi
  889. # end of 'src/list.c'
  890. fi
  891. if test -f 'src/main.c' -a "${1}" != "-c" ; then 
  892.   echo shar: Will not clobber existing file \"'src/main.c'\"
  893. else
  894. echo shar: Extracting \"'src/main.c'\" \(4705 characters\)
  895. sed "s/^X//" >'src/main.c' <<'END_OF_FILE'
  896. Xchar rcsid[] =
  897. X    "$Id: main.c,v 3.0 89/10/27 17:05:45 craig Exp $";
  898. X/*
  899. X * main.c
  900. X *
  901. X * Copyright (C) 1989, Craig E. Kolb
  902. X *
  903. X * This software may be freely copied, modified, and redistributed,
  904. X * provided that this copyright notice is preserved on all copies.
  905. X *
  906. X * There is no warranty or other guarantee of fitness for this software,
  907. X * it is provided solely .  Bug reports or fixes may be sent
  908. X * to the author, who may or may not act on them as he desires.
  909. X *
  910. X * You may not include this software in a program or other software product
  911. X * without supplying the source, or without informing the end-user that the
  912. X * source is available for no extra charge.
  913. X *
  914. X * If you modify this software, you should include a notice giving the
  915. X * name of the person performing the modification, the date of modification,
  916. X * and the reason for such modification.
  917. X *
  918. X * $Log:    main.c,v $
  919. X * Revision 3.0  89/10/27  17:05:45  craig
  920. X * Baseline for first official release.
  921. X * 
  922. X */
  923. X#include <stdio.h>
  924. X#include <signal.h>
  925. X#ifdef SYSV
  926. X#include <sys/types.h>
  927. X#include <sys/times.h>
  928. X#include <sys/param.h>
  929. X#else
  930. X#ifndef AZTEC_C
  931. X#include <sys/time.h>
  932. X#include <sys/resource.h>
  933. X#endif
  934. X#endif
  935. X#include "constants.h"
  936. X#include "typedefs.h"
  937. X#include "defaults.h"
  938. X
  939. Xunsigned long    EyeRays,    /* # of eye rays spawned */
  940. X        ShadowRays,    /* # of shadow rays spawned */
  941. X        ReflectRays,    /* # of reflected rays */
  942. X        RefractRays,    /* # of refracted rays */
  943. X        HitRays,    /* # of rays which hit something. */
  944. X        BVTests,    /* # of bounding volume tests. */
  945. X        SuperSampled;    /* # of supersampled pixels. */
  946. Xdouble        ftmp;        /* Used by fabs() macro. */
  947. XFILE        *fstats;    /* Statistics file */
  948. X
  949. X/*
  950. X * LINDA silliness.
  951. X */
  952. X#ifdef LINDA
  953. Xrayshade_main(argc, argv)
  954. X#else
  955. Xmain(argc, argv)
  956. X#endif
  957. Xint argc;
  958. Xchar **argv;
  959. X{
  960. X    double utime, stime;
  961. X    unsigned long TotalRays;
  962. X    extern int Verbose, Cache;
  963. X    extern unsigned long CacheWorked, CacheFailed, ShadowHits;
  964. X
  965. X    /*
  966. X      * Initialize variables, etc.
  967. X     */
  968. X    setup();
  969. X    /*
  970. X     * Parse options from command line.
  971. X     */
  972. X    parse_options(argc, argv);
  973. X    /*
  974. X     * Process input file.
  975. X     */
  976. X    if (Verbose) {
  977. X        print_version();
  978. X        fprintf(fstats,"Reading input file...\n");
  979. X        fflush(fstats);
  980. X    }
  981. X    read_input_file();
  982. X    /*
  983. X     * Set variables which weren't set on command line
  984. X     * or in input file.
  985. X     */
  986. X    cleanup();
  987. X    /*
  988. X     * Start new picture.
  989. X     */
  990. X    startpic();
  991. X    /*
  992. X     * Set up viewing parameters.
  993. X     */
  994. X    viewing();
  995. X    /*
  996. X     * Set up world.
  997. X     */
  998. X    if (Verbose)
  999. X        fprintf(fstats,"Setting up voxels...\n");
  1000. X    SetupWorld();
  1001. X    get_cpu_time(&utime, &stime);
  1002. X    fprintf(fstats,"Preprocessing time:\t");
  1003. X    fprintf(fstats,"%2.2lfu  %2.2lfs\n",utime, stime);
  1004. X    fprintf(fstats,"Starting trace.\n");
  1005. X    fflush(fstats);
  1006. X    /*
  1007. X     * Trace the image.
  1008. X     */
  1009. X    raytrace();
  1010. X    /*
  1011. X     * Close the image file.
  1012. X     */
  1013. X    endpic();
  1014. X
  1015. X    get_cpu_time(&utime, &stime);
  1016. X
  1017. X#ifndef LINDA
  1018. X    TotalRays = EyeRays + ShadowRays + ReflectRays + RefractRays;
  1019. X    ShadowHits += CacheWorked;
  1020. X    HitRays += ShadowHits;
  1021. X    fprintf(fstats,"Eye rays:\t\t\t%ld\n", EyeRays);
  1022. X    fprintf(fstats,"Shadow rays:\t\t\t%ld\n",ShadowRays);
  1023. X    fprintf(fstats,"Reflected rays:\t\t\t%ld\n",ReflectRays);
  1024. X    fprintf(fstats,"Refracted rays:\t\t\t%ld\n",RefractRays);
  1025. X    fprintf(fstats,"Total rays:\t\t\t%ld\n", TotalRays);
  1026. X    if (TotalRays != 0.)
  1027. X        fprintf(fstats,"Intersecting rays:\t\t%ld (%3.3f%%)\n",
  1028. X            HitRays, 100. * (float)HitRays / (float)TotalRays);
  1029. X    if (ShadowRays != 0) {
  1030. X        if (Cache)
  1031. X            fprintf(fstats,"Shadow cache hits:\t\t%ld (%ld misses)\n",
  1032. X                CacheWorked, CacheFailed);
  1033. X        fprintf(fstats,"Total shadow hits:\t\t%ld (%3.3f%%)\n",
  1034. X            ShadowHits, 100.*(float)ShadowHits / (float)ShadowRays);
  1035. X    }
  1036. X    fprintf(fstats,"Supersampled pixels:\t\t%ld\n",SuperSampled);
  1037. X    fprintf(fstats,"B.V. intersection tests:\t%ld\n", BVTests);
  1038. X    print_prim_stats();
  1039. X#endif
  1040. X    fprintf(fstats,"Total CPU time (sec):\t\t");
  1041. X    fprintf(fstats,"%2.2lf (%2.2lfu + %2.2lfs)\n",utime+stime, utime, stime);
  1042. X#ifndef LINDA
  1043. X    if (TotalRays != 0.)
  1044. X        fprintf(fstats,"Seconds / ray:\t\t\t%4.4lf\n",
  1045. X                (utime + stime) / (double)TotalRays);
  1046. X    if (HitRays != 0.)
  1047. X        fprintf(fstats, "Seconds / intersecting ray:\t%4.4lf\n",
  1048. X                (utime + stime) / (double)HitRays);
  1049. X#endif
  1050. X    PrintMemoryStats();
  1051. X    exit(0);
  1052. X}
  1053. X
  1054. X#ifdef SYSV
  1055. Xget_cpu_time(utime, stime)
  1056. Xdouble *utime, *stime;
  1057. X{
  1058. X    struct tms time;
  1059. X    long times();
  1060. X
  1061. X    (void)times(&time);
  1062. X    *utime = (double)time.tms_utime / (double)HZ;
  1063. X    *stime = (double)time.tms_stime / (double)HZ;
  1064. X}
  1065. X#else
  1066. X#ifdef AZTEC_C
  1067. Xget_cpu_time()
  1068. X{
  1069. X}
  1070. X#else    /* !SYSV && !AZTEC_C */
  1071. Xget_cpu_time(utime, stime)
  1072. Xdouble *utime, *stime;
  1073. X{
  1074. X    struct rusage usage;
  1075. X
  1076. X    getrusage(RUSAGE_SELF, &usage);
  1077. X
  1078. X    *utime = (double)usage.ru_utime.tv_sec +
  1079. X            (double)usage.ru_utime.tv_usec / 1000000.;
  1080. X    *stime = (double)usage.ru_stime.tv_sec +
  1081. X            (double)usage.ru_stime.tv_usec / 1000000.;
  1082. X}
  1083. X#endif
  1084. X#endif
  1085. END_OF_FILE
  1086. if test 4705 -ne `wc -c <'src/main.c'`; then
  1087.     echo shar: \"'src/main.c'\" unpacked with wrong size!
  1088. fi
  1089. # end of 'src/main.c'
  1090. fi
  1091. if test -f 'src/outputp.c' -a "${1}" != "-c" ; then 
  1092.   echo shar: Will not clobber existing file \"'src/outputp.c'\"
  1093. else
  1094. echo shar: Extracting \"'src/outputp.c'\" \(4181 characters\)
  1095. sed "s/^X//" >'src/outputp.c' <<'END_OF_FILE'
  1096. X/*
  1097. X * outputp.c
  1098. X *
  1099. X * Copyright (C) 1989, Craig E. Kolb
  1100. X *
  1101. X * This software may be freely copied, modified, and redistributed,
  1102. X * provided that this copyright notice is preserved on all copies.
  1103. X *
  1104. X * There is no warranty or other guarantee of fitness for this software,
  1105. X * it is provided solely .  Bug reports or fixes may be sent
  1106. X * to the author, who may or may not act on them as he desires.
  1107. X *
  1108. X * You may not include this software in a program or other software product
  1109. X * without supplying the source, or without informing the end-user that the
  1110. X * source is available for no extra charge.
  1111. X *
  1112. X * If you modify this software, you should include a notice giving the
  1113. X * name of the person performing the modification, the date of modification,
  1114. X * and the reason for such modification.
  1115. X *
  1116. X * $Id: outputp.c,v 3.0 89/10/27 02:05:58 craig Exp $
  1117. X *
  1118. X * $Log:    outputp.c,v $
  1119. X * Revision 3.0  89/10/27  02:05:58  craig
  1120. X * Baseline for first official release.
  1121. X * 
  1122. X */
  1123. X#include <stdio.h>
  1124. X#include "typedefs.h"
  1125. X#include "constants.h"
  1126. X#include "funcdefs.h"
  1127. X#ifndef NORLE
  1128. X#include "svfb_global.h"
  1129. Xunsigned char *buffer[3];    /* Output buffer */
  1130. X#endif
  1131. X
  1132. XFILE *imgfile;            /* Raster output file */
  1133. Xchar outfilename[BUFSIZ];    /* Name of output file */
  1134. Xint Appending;            /* Appending to output file? */
  1135. X
  1136. X/*
  1137. X * Convert floating-point to unsigned char (0-255).
  1138. X * This could easily be a macro, but the old SGI compilers dump core
  1139. X * on it for some reason.
  1140. X */
  1141. Xunsigned char
  1142. Xcorrect(x)
  1143. Xdouble x;
  1144. X{
  1145. X    if (x < 0)
  1146. X        return 0;
  1147. X    if (x > 255)
  1148. X        return 255;
  1149. X    return x;
  1150. X}
  1151. X
  1152. X#ifndef NORLE
  1153. X/*
  1154. X * Open image file and write RLE header.
  1155. X */
  1156. Xstartpic()
  1157. X{
  1158. X    extern int Xres, Yres;
  1159. X
  1160. X    if (*outfilename) {
  1161. X        if (Appending) {
  1162. X            /*
  1163. X             * If we're appending, we *still* have to write
  1164. X             * the Utah Raster header to the file.  This is
  1165. X             * due to strangeness in the Utah Raster toolkit,
  1166. X             * which does some vital initialization in sv_setup().
  1167. X             */
  1168. X            imgfile = fopen(outfilename, "r+");
  1169. X        } else
  1170. X            imgfile = fopen(outfilename, "w");
  1171. X        if (imgfile == (FILE *)NULL) {
  1172. X            fprintf(stderr,"Cannot open %s for writing.\n",
  1173. X                            outfilename);
  1174. X            exit(2);
  1175. X        }
  1176. X    } else
  1177. X        imgfile = stdout;
  1178. X    sv_globals.svfb_fd = imgfile;
  1179. X    sv_globals.sv_xmax = Xres -1;
  1180. X    sv_globals.sv_ymax = Yres -1;
  1181. X    sv_globals.sv_xmin = sv_globals.sv_ymin = 0;
  1182. X    sv_globals.sv_ncolors = 3;
  1183. X    sv_globals.sv_alpha = 0;
  1184. X    sv_setup(RUN_DISPATCH, &sv_globals);
  1185. X    /*
  1186. X     * Flush the header.  If we don't, and LINDA forks off
  1187. X     * a bunch of workers, strange things will happen (they'll
  1188. X     * all flush the buffer when they die, and you end up with
  1189. X     * lots of headers at the end of the file).
  1190. X     */
  1191. X    fflush(sv_globals.svfb_fd);
  1192. X    buffer[0]=(unsigned char *)Malloc(sizeof(unsigned char)*(unsigned)Xres);
  1193. X    buffer[1]=(unsigned char *)Malloc(sizeof(unsigned char)*(unsigned)Xres);
  1194. X    buffer[2]=(unsigned char *)Malloc(sizeof(unsigned char)*(unsigned)Xres);
  1195. X    if (Appending)
  1196. X        fseek(imgfile, 0L, 2);        /* Go to end of file */
  1197. X}
  1198. X
  1199. X
  1200. X/*
  1201. X * Write a scanline of output.
  1202. X * "buf" is an array of Color structures of size Xres.  Each color
  1203. X * component is normalized to [0, 1.].
  1204. X */
  1205. Xoutline(buf)
  1206. XColor *buf;
  1207. X{
  1208. X    register int i;
  1209. X    extern int Xres;
  1210. X
  1211. X    for(i = 0;i < Xres;i++) {
  1212. X        /*
  1213. X         * Scale colors to fit unsigned char and check for
  1214. X         * over/underflow.
  1215. X         */
  1216. X        buffer[0][i] = correct(255 * buf[i].r);
  1217. X        buffer[1][i] = correct(255 * buf[i].g);
  1218. X        buffer[2][i] = correct(255 * buf[i].b);
  1219. X    }
  1220. X    sv_putrow(buffer, Xres, &sv_globals);
  1221. X}
  1222. X
  1223. X/*
  1224. X * Close image file.
  1225. X */
  1226. Xendpic()
  1227. X{
  1228. X    sv_puteof(&sv_globals);
  1229. X    fclose(imgfile);
  1230. X}
  1231. X
  1232. X#else /* NORLE */
  1233. X
  1234. Xstartpic()
  1235. X{
  1236. X    extern int Xres, Yres;
  1237. X
  1238. X    if (*outfilename) {
  1239. X        if (Appending)
  1240. X            imgfile = fopen(outfilename, "a");
  1241. X        else
  1242. X            imgfile = fopen(outfilename, "w");
  1243. X        if (imgfile == (FILE *)NULL) {
  1244. X            fprintf(stderr,"Cannot open %s for writing.\n",
  1245. X                            outfilename);
  1246. X            exit(2);
  1247. X        }
  1248. X    } else
  1249. X        imgfile = stdout;
  1250. X
  1251. X    fprintf(imgfile,"%d %d\n",Xres, Yres);
  1252. X    fflush(imgfile);
  1253. X}
  1254. X
  1255. Xoutline(buf)
  1256. XColor *buf;
  1257. X{
  1258. X    register int i;
  1259. X    extern int Xres;
  1260. X
  1261. X    for (i = 0; i < Xres; i++) {
  1262. X        fputc(correct(255.*buf[i].r), imgfile);
  1263. X        fputc(correct(255.*buf[i].g), imgfile);
  1264. X        fputc(correct(255.*buf[i].b), imgfile);
  1265. X    }
  1266. X    fflush(imgfile);
  1267. X}
  1268. X
  1269. Xendpic()
  1270. X{
  1271. X    fclose(imgfile);
  1272. X}
  1273. X#endif /* NORLE */
  1274. X
  1275. END_OF_FILE
  1276. if test 4181 -ne `wc -c <'src/outputp.c'`; then
  1277.     echo shar: \"'src/outputp.c'\" unpacked with wrong size!
  1278. fi
  1279. # end of 'src/outputp.c'
  1280. fi
  1281. if test -f 'src/primobj.h' -a "${1}" != "-c" ; then 
  1282.   echo shar: Will not clobber existing file \"'src/primobj.h'\"
  1283. else
  1284. echo shar: Extracting \"'src/primobj.h'\" \(3420 characters\)
  1285. sed "s/^X//" >'src/primobj.h' <<'END_OF_FILE'
  1286. X/*
  1287. X * primobj.h
  1288. X *
  1289. X * Copyright (C) 1989, Craig E. Kolb
  1290. X *
  1291. X * This software may be freely copied, modified, and redistributed,
  1292. X * provided that this copyright notice is preserved on all copies.
  1293. X *
  1294. X * There is no warranty or other guarantee of fitness for this software,
  1295. X * it is provided solely .  Bug reports or fixes may be sent
  1296. X * to the author, who may or may not act on them as he desires.
  1297. X *
  1298. X * You may not include this software in a program or other software product
  1299. X * without supplying the source, or without informing the end-user that the
  1300. X * source is available for no extra charge.
  1301. X *
  1302. X * If you modify this software, you should include a notice giving the
  1303. X * name of the person performing the modification, the date of modification,
  1304. X * and the reason for such modification.
  1305. X *
  1306. X * $Id: primobj.h,v 3.0 89/10/27 02:06:00 craig Exp $
  1307. X *
  1308. X * $Log:    primobj.h,v $
  1309. X * Revision 3.0  89/10/27  02:06:00  craig
  1310. X * Baseline for first official release.
  1311. X * 
  1312. X */
  1313. X
  1314. X/*
  1315. X * Cylinder
  1316. X */
  1317. Xtypedef struct cylinder    {
  1318. X    double rad, len;        /* Radius and length. */
  1319. X} Cylinder;
  1320. X
  1321. Xtypedef struct cone {
  1322. X    double start_pos, end_pos, apex_rad;    /* End cap Z positions */
  1323. X    double tantheta;            /* "slope" */
  1324. X} Cone;
  1325. X
  1326. X/*
  1327. X * Sphere
  1328. X */
  1329. Xtypedef struct {
  1330. X    double r;        /* radius   */
  1331. X    double x, y, z;        /* position */
  1332. X} Sphere;
  1333. X
  1334. X/*
  1335. X * Box
  1336. X */
  1337. Xtypedef struct {
  1338. X    double bounds[2][3];    /* Box corners */
  1339. X} Box;
  1340. X
  1341. X/*
  1342. X * Triangle
  1343. X */
  1344. Xtypedef struct {
  1345. X    Vector nrm;        /* triangle normal */
  1346. X    Vector p1, p2, p3;    /* vertices */
  1347. X    double d;        /* plane constant  */
  1348. X    char index;        /* Flag used for shading/intersection test. */
  1349. X    Vector *vnorm;        /* Array of vertex normals */
  1350. X    double *b;        /* Array of barycentric coordinates */
  1351. X    Vector e1, e2, e3;    /* edge vectors */
  1352. X} Triangle;
  1353. X
  1354. X/*
  1355. X * Polygon
  1356. X */
  1357. Xtypedef struct {
  1358. X    Vector norm;        /* Normal to polygon */
  1359. X    double d;        /* Plane constant */
  1360. X    char index;        /* Which normal coord is "dominant"? */
  1361. X    Vector *points;        /* Array of vertices */
  1362. X    int npoints;        /* Number of vertices */
  1363. X} Polygon;
  1364. X
  1365. X/*
  1366. X * Superquadric
  1367. X */
  1368. Xtypedef struct {
  1369. X    double bounds[2][3];    /* bounding box */
  1370. X    double x, y, z;        /* Center */
  1371. X    double pow;        /* Superquadric power */
  1372. X    double a, b, c, r;
  1373. X    double err;        /* Error constant */
  1374. X} Superq;
  1375. X
  1376. X/*
  1377. X * Plane
  1378. X */
  1379. Xtypedef struct {
  1380. X    Vector norm;    /* Plane normal. */
  1381. X    double d;    /* Plane constant. */
  1382. X} Plane;
  1383. X
  1384. X/*
  1385. X * Height field gunk.
  1386. X */
  1387. Xtypedef struct hfTri {
  1388. X    Vector v1, v2, v3, norm;
  1389. X    double d;
  1390. X    char type;
  1391. X    struct hfTri *next, *prev;
  1392. X} hfTri;
  1393. X
  1394. Xtypedef struct {
  1395. X    int len;
  1396. X    hfTri *head, *tail;
  1397. X} TriQueue;
  1398. X
  1399. Xtypedef struct {
  1400. X    float **data;        /* Altitude points */
  1401. X    float minz, maxz;
  1402. X    int size, *lsize;    /* # of points/side */
  1403. X    int BestSize;         /* "best" division size */
  1404. X    float iBestSize;    /* inverse of above (for faster computation) */
  1405. X    int levels;        /* log base BestSize of size */
  1406. X    float ***boundsmax;    /* high data values at various resolutions. */
  1407. X    float ***boundsmin;
  1408. X    float *spacing;
  1409. X    hfTri hittri, **q;    /* hit triangle and triangle cache */
  1410. X    int qtail, qsize;    /* end and length of cache */
  1411. X    double boundbox[2][3];    /* bounding box of HF */
  1412. X} Hf;
  1413. X
  1414. X/*
  1415. X * Primitive object.
  1416. X */
  1417. Xtypedef struct Primitive {
  1418. X    char type;            /* object type */
  1419. X    struct Surface *surf;        /* default surface */
  1420. X    union {
  1421. X        Sphere        *p_sphere;
  1422. X        Box         *p_box;
  1423. X        Triangle    *p_triangle;
  1424. X        Superq        *p_superq;
  1425. X        Plane        *p_plane;
  1426. X        Cylinder    *p_cylinder;
  1427. X        Polygon        *p_poly;
  1428. X        Cone        *p_cone;
  1429. X        Hf        *p_hf;
  1430. X    } objpnt;    /* Pointer to primitive */
  1431. X} Primitive;
  1432. END_OF_FILE
  1433. if test 3420 -ne `wc -c <'src/primobj.h'`; then
  1434.     echo shar: \"'src/primobj.h'\" unpacked with wrong size!
  1435. fi
  1436. # end of 'src/primobj.h'
  1437. fi
  1438. if test -f 'src/setup.c' -a "${1}" != "-c" ; then 
  1439.   echo shar: Will not clobber existing file \"'src/setup.c'\"
  1440. else
  1441. echo shar: Extracting \"'src/setup.c'\" \(4525 characters\)
  1442. sed "s/^X//" >'src/setup.c' <<'END_OF_FILE'
  1443. X/*
  1444. X * setup.c
  1445. X *
  1446. X * Copyright (C) 1989, Craig E. Kolb
  1447. X *
  1448. X * This software may be freely copied, modified, and redistributed,
  1449. X * provided that this copyright notice is preserved on all copies.
  1450. X *
  1451. X * There is no warranty or other guarantee of fitness for this software,
  1452. X * it is provided solely .  Bug reports or fixes may be sent
  1453. X * to the author, who may or may not act on them as he desires.
  1454. X *
  1455. X * You may not include this software in a program or other software product
  1456. X * without supplying the source, or without informing the end-user that the
  1457. X * source is available for no extra charge.
  1458. X *
  1459. X * If you modify this software, you should include a notice giving the
  1460. X * name of the person performing the modification, the date of modification,
  1461. X * and the reason for such modification.
  1462. X *
  1463. X * $Id: setup.c,v 3.0 89/10/27 02:06:03 craig Exp $
  1464. X *
  1465. X * $Log:    setup.c,v $
  1466. X * Revision 3.0  89/10/27  02:06:03  craig
  1467. X * Baseline for first official release.
  1468. X * 
  1469. X */
  1470. X#include <stdio.h>
  1471. X#include "constants.h"
  1472. X#include "defaults.h"
  1473. X#include "typedefs.h"
  1474. X#include "funcdefs.h"
  1475. X
  1476. X#ifdef MULTIMAX
  1477. X#include <parallel.h>
  1478. X#define SHARED_BYTES    23    /* 2^23 bytes of shared memory */
  1479. X#endif
  1480. X/*
  1481. X * Set default parameters
  1482. X */
  1483. Xsetup()
  1484. X{
  1485. X    extern int maxlevel;
  1486. X    extern double hfov, vfov;
  1487. X    extern Vector eyep, lookp, up;
  1488. X    extern Object *World;
  1489. X    extern ObjList *CurObj;
  1490. X#ifdef MULTIMAX
  1491. X    unsigned int bytes;
  1492. X
  1493. X    /*
  1494. X     * Initialize shared memory stuff.
  1495. X     */
  1496. X    bytes = 1 << SHARED_BYTES;
  1497. X    if (share_malloc_init(bytes) == -1) {
  1498. X        fprintf(stderr,"Cannot share_malloc %d bytes.\n",bytes);
  1499. X        exit(10);
  1500. X    } else
  1501. X        fprintf(stderr,"Malloced %d bytes of shared memory.\n",
  1502. X                bytes);
  1503. X#endif
  1504. X    /*
  1505. X     * Like every other object, the objects that make up
  1506. X     * "World" are stored in a simple linked list until it
  1507. X     * has been completely defined.  Once the definition is
  1508. X     * complete we convert the linked list into a Grid or List
  1509. X     * as per the user's wishes.
  1510. X     */
  1511. X    World = new_object("world", LIST, (char *)NULL, (Trans *)NULL);
  1512. X
  1513. X    maxlevel = MAXLEVEL;
  1514. X    hfov = HFOV;
  1515. X    vfov = UNSET;
  1516. X    eyep.x = EYEX;
  1517. X    eyep.y = EYEY;
  1518. X    eyep.z = EYEZ;
  1519. X    lookp.x = LOOKX;
  1520. X    lookp.y = LOOKY;
  1521. X    lookp.z = LOOKZ;
  1522. X    up.x = UPX;
  1523. X    up.y = UPY;
  1524. X    up.z = UPZ;
  1525. X
  1526. X    /*
  1527. X     * Kinda yicky, but compatible.
  1528. X     */
  1529. X    CurObj = (ObjList *)Malloc(sizeof(ObjList));
  1530. X    CurObj->next = (ObjList *)0;
  1531. X    CurObj->data = (Object *)0;
  1532. X    InitRTable();            /* Initialize values for Noise() */
  1533. X}
  1534. X
  1535. X/*
  1536. X * cleanup()
  1537. X *
  1538. X * Initialize variables not set on command line or in input file.
  1539. X */
  1540. Xcleanup()
  1541. X{
  1542. X    int i;
  1543. X    extern int nlight, maxlevel;
  1544. X    extern int Xres, Yres, StartLine, Jittered, JitSamples, pixel_div;
  1545. X    extern double RedContrast, GreenContrast, BlueContrast, TreeCutoff;
  1546. X    extern double vfov, hfov;
  1547. X    extern Light light[];
  1548. X    extern FILE *fstats;
  1549. X
  1550. X    /*
  1551. X     * Because we want the user to be able to override the input file
  1552. X     * through the command line, we have to initialize some variables to
  1553. X     * bogus values so that when the file is being parsed, it is
  1554. X     * possible to tell if a given variable has been set on the
  1555. X     * command line.
  1556. X     *
  1557. X     * If such variables are not set to legal values on the command
  1558. X     * line or in the input file, we must do it here.
  1559. X     */
  1560. X    if (Xres == UNSET)
  1561. X        Xres = XRESOLUTION;
  1562. X    if (Yres == UNSET)
  1563. X        Yres = YRESOLUTION;
  1564. X
  1565. X    if (StartLine == UNSET)
  1566. X        StartLine = Yres -1;
  1567. X    else
  1568. X        fprintf(fstats,"Starting on line %d\n",StartLine);
  1569. X
  1570. X    /*
  1571. X     * If not defined in the input file, calculate VFOV
  1572. X     * by hand.  This assumes that pixels are square, which is
  1573. X     * probably a bad idea.  ("aspect" option?)
  1574. X     */
  1575. X    if (vfov == UNSET)
  1576. X        vfov = hfov * Yres / Xres;
  1577. X
  1578. X    if (!Jittered && pixel_div == UNSET)
  1579. X        pixel_div = PIXEL_DIV;
  1580. X
  1581. X    if (JitSamples == UNSET)
  1582. X        JitSamples = DEFLIGHTSAMPLES;
  1583. X
  1584. X    if (Jittered && JitSamples > 5) {
  1585. X        fprintf(stderr,"Sorry, %d rays/pixel not supported.\n",
  1586. X                        JitSamples*JitSamples);
  1587. X        exit(2);
  1588. X    }
  1589. X    if (!Jittered && JitSamples < 3) {
  1590. X        fprintf(stderr,"Samples (-S) value must be at least 3.\n");
  1591. X        exit(2);
  1592. X    }
  1593. X
  1594. X    if (TreeCutoff == UNSET)
  1595. X        TreeCutoff = DEFCUTOFF;
  1596. X
  1597. X    if (RedContrast == UNSET)
  1598. X        RedContrast = DEFREDCONT;
  1599. X    if (GreenContrast == UNSET)
  1600. X        GreenContrast = DEFGREENCONT;
  1601. X    if (BlueContrast == UNSET)
  1602. X        BlueContrast = DEFBLUECONT;
  1603. X
  1604. X    /*
  1605. X     * Now that we've parsed the input file, we know what
  1606. X     * maxlevel is, so we can allocate the right amount of
  1607. X     * space for each light source's cache and related
  1608. X     * transformation.
  1609. X     */
  1610. X    for (i = 0; i < nlight; i++) {
  1611. X        light[i].cache = (Primitive **)Calloc(maxlevel+1,
  1612. X                    sizeof(Primitive *));
  1613. X        light[i].trans = (TransInfo *)Malloc((maxlevel+1)*
  1614. X                    sizeof(TransInfo));
  1615. X    }
  1616. X}
  1617. END_OF_FILE
  1618. if test 4525 -ne `wc -c <'src/setup.c'`; then
  1619.     echo shar: \"'src/setup.c'\" unpacked with wrong size!
  1620. fi
  1621. # end of 'src/setup.c'
  1622. fi
  1623. if test -f 'src/sphere.c' -a "${1}" != "-c" ; then 
  1624.   echo shar: Will not clobber existing file \"'src/sphere.c'\"
  1625. else
  1626. echo shar: Extracting \"'src/sphere.c'\" \(3346 characters\)
  1627. sed "s/^X//" >'src/sphere.c' <<'END_OF_FILE'
  1628. X/*
  1629. X * sphere.c
  1630. X *
  1631. X * Copyright (C) 1989, Craig E. Kolb
  1632. X *
  1633. X * This software may be freely copied, modified, and redistributed,
  1634. X * provided that this copyright notice is preserved on all copies.
  1635. X *
  1636. X * There is no warranty or other guarantee of fitness for this software,
  1637. X * it is provided solely .  Bug reports or fixes may be sent
  1638. X * to the author, who may or may not act on them as he desires.
  1639. X *
  1640. X * You may not include this software in a program or other software product
  1641. X * without supplying the source, or without informing the end-user that the
  1642. X * source is available for no extra charge.
  1643. X *
  1644. X * If you modify this software, you should include a notice giving the
  1645. X * name of the person performing the modification, the date of modification,
  1646. X * and the reason for such modification.
  1647. X *
  1648. X * $Id: sphere.c,v 3.0 89/10/27 02:06:04 craig Exp $
  1649. X *
  1650. X * $Log:    sphere.c,v $
  1651. X * Revision 3.0  89/10/27  02:06:04  craig
  1652. X * Baseline for first official release.
  1653. X * 
  1654. X */
  1655. X#include <math.h>
  1656. X#include <stdio.h>
  1657. X#include "constants.h"
  1658. X#include "typedefs.h"
  1659. X#include "funcdefs.h"
  1660. X
  1661. X/*
  1662. X * Create & return reference to a sphere.
  1663. X */
  1664. XObject *
  1665. Xmaksph(surf, r, pos)
  1666. Xchar    *surf;
  1667. Xdouble r;
  1668. XVector *pos;
  1669. X{
  1670. X    Sphere       *sphere;
  1671. X    Object *newobj;
  1672. X    Primitive *prim;
  1673. X    extern int Quiet, yylineno;
  1674. X
  1675. X    if (r < EPSILON) {
  1676. X        if (!Quiet)
  1677. X            fprintf(stderr,"Degenerate sphere (line %d)\n",
  1678. X                            yylineno);
  1679. X        return (Object *)0;
  1680. X    }
  1681. X
  1682. X    prim = mallocprim();
  1683. X    newobj = new_object(NULL, SPHERE, (char *)prim, (Trans *)NULL);
  1684. X    prim->type = SPHERE;
  1685. X    prim->surf = find_surface(surf);
  1686. X    sphere = (Sphere *)Malloc(sizeof(Sphere));
  1687. X    prim->objpnt.p_sphere = sphere;
  1688. X    /*
  1689. X     * sphere->r really holds the square of the radius.
  1690. X     */
  1691. X    sphere->r = r*r;
  1692. X    sphere->x = pos->x;
  1693. X    sphere->y = pos->y;
  1694. X    sphere->z = pos->z;
  1695. X
  1696. X    return newobj;
  1697. X}
  1698. X
  1699. X/*
  1700. X * Ray/sphere intersection test.
  1701. X */
  1702. Xdouble
  1703. Xintsph(pos, ray, obj)
  1704. Xregister Vector           *pos, *ray;
  1705. XPrimitive       *obj;
  1706. X{
  1707. X    Sphere *sph;
  1708. X    double xadj, yadj, zadj;
  1709. X    double          b, t, s;
  1710. X    extern unsigned long primtests[];
  1711. X
  1712. X    primtests[SPHERE]++;
  1713. X    sph = obj->objpnt.p_sphere;
  1714. X    /*
  1715. X     * Translate ray origin to object space and negate everything.
  1716. X     * (Thus, we translate the sphere into ray space, which saves
  1717. X     * us a couple of negations below.)
  1718. X     */
  1719. X    xadj = sph->x - pos->x;
  1720. X    yadj = sph->y - pos->y;
  1721. X    zadj = sph->z - pos->z;
  1722. X
  1723. X    /*
  1724. X     * Solve quadratic equiation.  Recall that sph->r is the square of r.
  1725. X     */
  1726. X    b = xadj * ray->x + yadj * ray->y + zadj * ray->z;
  1727. X    t = b * b - xadj * xadj - yadj * yadj - zadj * zadj + sph->r;
  1728. X    if (t < 0)
  1729. X        return 0.;
  1730. X    t = sqrt(t);
  1731. X    s = b - t;
  1732. X    if (s > EPSILON)
  1733. X        return s;
  1734. X    s = b + t;
  1735. X    if (s > EPSILON)
  1736. X        return s;
  1737. X    return 0.;
  1738. X}
  1739. X
  1740. Xnrmsph(pos, obj, nrm)
  1741. XVector           *pos, *nrm;
  1742. XPrimitive       *obj;
  1743. X{
  1744. X    /*
  1745. X     * Don't bother dividing by by sphere->r -- the normal will be
  1746. X     * normalized later.
  1747. X     */
  1748. X    nrm->x = (pos->x - obj->objpnt.p_sphere->x);
  1749. X    nrm->y = (pos->y - obj->objpnt.p_sphere->y);
  1750. X    nrm->z = (pos->z - obj->objpnt.p_sphere->z);
  1751. X}
  1752. X
  1753. Xsphextent(o, bounds)
  1754. XPrimitive *o;
  1755. Xdouble bounds[2][3];
  1756. X{
  1757. X    Sphere *s = o->objpnt.p_sphere;
  1758. X    double r;
  1759. X
  1760. X    /*
  1761. X     * Could store both r*r and r, but sphextent is only
  1762. X     * called in preprocessing, so...
  1763. X     */
  1764. X    r = sqrt(s->r);
  1765. X    bounds[LOW][X] = s->x - r;
  1766. X    bounds[HIGH][X] = s->x + r;
  1767. X    bounds[LOW][Y] = s->y - r;
  1768. X    bounds[HIGH][Y] = s->y + r;
  1769. X    bounds[LOW][Z] = s->z - r;
  1770. X    bounds[HIGH][Z] = s->z + r;
  1771. X}
  1772. END_OF_FILE
  1773. if test 3346 -ne `wc -c <'src/sphere.c'`; then
  1774.     echo shar: \"'src/sphere.c'\" unpacked with wrong size!
  1775. fi
  1776. # end of 'src/sphere.c'
  1777. fi
  1778. if test -f 'src/superq.c' -a "${1}" != "-c" ; then 
  1779.   echo shar: Will not clobber existing file \"'src/superq.c'\"
  1780. else
  1781. echo shar: Extracting \"'src/superq.c'\" \(3390 characters\)
  1782. sed "s/^X//" >'src/superq.c' <<'END_OF_FILE'
  1783. X/*
  1784. X * superq.c
  1785. X *
  1786. X * Slightly modified version of Kuchkuda's superquadric code.
  1787. X *
  1788. X * $Id: superq.c,v 3.0 89/10/27 02:06:05 craig Exp $
  1789. X *
  1790. X * $Log:    superq.c,v $
  1791. X * Revision 3.0  89/10/27  02:06:05  craig
  1792. X * Baseline for first official release.
  1793. X * 
  1794. X */
  1795. X#include <math.h>
  1796. X#include "constants.h"
  1797. X#include "typedefs.h"
  1798. X#include "funcdefs.h"
  1799. X
  1800. X#define ERRCONST 1.006
  1801. X
  1802. X/*
  1803. X * Create and return reference to a superquadric.
  1804. X */
  1805. XObject *
  1806. Xmaksup(surf, x, y, z, xs, ys, zs, power)
  1807. Xchar    *surf;
  1808. Xdouble    x, y, z, xs, ys, zs, power;
  1809. X{
  1810. X    double          maxval;
  1811. X    Superq       *super;
  1812. X    Primitive    *prim;
  1813. X    Object *newobj;
  1814. X
  1815. X    prim = mallocprim();
  1816. X    prim->surf = find_surface(surf);
  1817. X    newobj = new_object(NULL, SUPERQ, (char *)prim, (Trans *)NULL);
  1818. X    prim->type = SUPERQ;
  1819. X    super = (Superq *) Malloc((unsigned)sizeof(Superq));
  1820. X    prim->objpnt.p_superq = super;
  1821. X    super->x = x;
  1822. X    super->y = y;
  1823. X    super->z = z;
  1824. X    super->bounds[LOW][X] = x - xs;
  1825. X    super->bounds[HIGH][X] = x + xs;
  1826. X    super->bounds[LOW][Y] = y - ys;
  1827. X    super->bounds[HIGH][Y] = y + ys;
  1828. X    super->bounds[LOW][Z] = z - zs;
  1829. X    super->bounds[HIGH][Z] = z + zs;
  1830. X
  1831. X    super->pow = power;
  1832. X    maxval = xs;
  1833. X    if (ys > maxval)
  1834. X        maxval = ys;
  1835. X    if (zs > maxval)
  1836. X        maxval = zs;
  1837. X    super->a = xs / maxval;
  1838. X    super->b = ys / maxval;
  1839. X    super->c = zs / maxval;
  1840. X    super->r = pow(maxval, power);
  1841. X    super->err = pow((ERRCONST * maxval), power) - super->r;
  1842. X
  1843. X    return newobj;
  1844. X}
  1845. X
  1846. Xdouble
  1847. Xintsup(pos, ray, obj)
  1848. XVector           *pos, *ray;
  1849. XPrimitive       *obj;
  1850. X{
  1851. X    double          s, si, t;
  1852. X    double          xadj, yadj, zadj;
  1853. X    double          old, result, p;
  1854. X    Ray        tmpray;
  1855. X    Superq        *super;
  1856. X    extern        unsigned long primtests[];
  1857. X
  1858. X    primtests[SUPERQ]++;
  1859. X    super = obj->objpnt.p_superq;
  1860. X    /* find box intersection */
  1861. X    if (OutOfBounds(pos, super->bounds)) {
  1862. X        tmpray.pos = *pos;
  1863. X        tmpray.dir = *ray;
  1864. X        s = IntBounds(&tmpray, super->bounds);
  1865. X        if (s <= 0.)
  1866. X            return 0;
  1867. X    }
  1868. X    else
  1869. X        s = 0.;
  1870. X
  1871. X    xadj = pos->x - super->x;
  1872. X    yadj = pos->y - super->y;
  1873. X    zadj = pos->z - super->z;
  1874. X
  1875. X    /* initial solution */
  1876. X    p = super->pow;
  1877. X    result = pow(fabs((xadj + ray->x * s) / super->a), p)
  1878. X        + pow(fabs((yadj + ray->y * s) / super->b), p)
  1879. X        + pow(fabs((zadj + ray->z * s) / super->c), p)
  1880. X        - super->r;
  1881. X
  1882. X    si = s;
  1883. X    s = s + 0.001;
  1884. X    /* iterative refinement */
  1885. X    while (result > super->err) {
  1886. X        old = result;
  1887. X        result = pow(fabs((xadj + ray->x * s) / super->a), p)
  1888. X            + pow(fabs((yadj + ray->y * s) / super->b), p)
  1889. X            + pow(fabs((zadj + ray->z * s) / super->c), p)
  1890. X            - super->r;
  1891. X
  1892. X        if (result >= old)
  1893. X            return (0.0);
  1894. X        t = (result * (s - si)) / (result - old);
  1895. X        si = s;
  1896. X        s -= t;
  1897. X    }
  1898. X
  1899. X    return (s);
  1900. X}
  1901. X
  1902. Xnrmsup(pos, obj, nrm)
  1903. XVector           *pos, *nrm;
  1904. XPrimitive       *obj;
  1905. X{
  1906. X    double k;
  1907. X    Superq *super;
  1908. X
  1909. X    super = obj->objpnt.p_superq;
  1910. X    nrm->x = (pos->x - super->x) / super->a;
  1911. X    nrm->y = (pos->y - super->y) / super->b;
  1912. X    nrm->z = (pos->z - super->z) / super->c;
  1913. X    k = super->pow - 1;
  1914. X    if (nrm->x > 0)
  1915. X        nrm->x = pow(nrm->x, k);
  1916. X    else
  1917. X        nrm->x = -pow(-nrm->x, k);
  1918. X    if (nrm->y > 0)
  1919. X        nrm->y = pow(nrm->y, k);
  1920. X    else
  1921. X        nrm->y = -pow(-nrm->y, k);
  1922. X    if (nrm->z > 0)
  1923. X        nrm->z = pow(nrm->z, k);
  1924. X    else
  1925. X        nrm->z = -pow(-nrm->z, k);
  1926. X}
  1927. X
  1928. X
  1929. Xsupextent(o, bounds)
  1930. XPrimitive *o;
  1931. Xdouble bounds[2][3];
  1932. X{
  1933. X    Superq *s = o->objpnt.p_superq;
  1934. X
  1935. X    bounds[LOW][X] = s->bounds[LOW][X];
  1936. X    bounds[HIGH][X] = s->bounds[HIGH][X];
  1937. X    bounds[LOW][Y] = s->bounds[LOW][Y];
  1938. X    bounds[HIGH][Y] = s->bounds[HIGH][Y];
  1939. X    bounds[LOW][Z] = s->bounds[LOW][Z];
  1940. X    bounds[HIGH][Z] = s->bounds[HIGH][Z];
  1941. X}
  1942. END_OF_FILE
  1943. if test 3390 -ne `wc -c <'src/superq.c'`; then
  1944.     echo shar: \"'src/superq.c'\" unpacked with wrong size!
  1945. fi
  1946. # end of 'src/superq.c'
  1947. fi
  1948. if test -f 'src/surface.c' -a "${1}" != "-c" ; then 
  1949.   echo shar: Will not clobber existing file \"'src/surface.c'\"
  1950. else
  1951. echo shar: Extracting \"'src/surface.c'\" \(3946 characters\)
  1952. sed "s/^X//" >'src/surface.c' <<'END_OF_FILE'
  1953. X/*
  1954. X * surface.c
  1955. X *
  1956. X * Copyright (C) 1989, Craig E. Kolb
  1957. X *
  1958. X * This software may be freely copied, modified, and redistributed,
  1959. X * provided that this copyright notice is preserved on all copies.
  1960. X *
  1961. X * There is no warranty or other guarantee of fitness for this software,
  1962. X * it is provided solely .  Bug reports or fixes may be sent
  1963. X * to the author, who may or may not act on them as he desires.
  1964. X *
  1965. X * You may not include this software in a program or other software product
  1966. X * without supplying the source, or without informing the end-user that the
  1967. X * source is available for no extra charge.
  1968. X *
  1969. X * If you modify this software, you should include a notice giving the
  1970. X * name of the person performing the modification, the date of modification,
  1971. X * and the reason for such modification.
  1972. X *
  1973. X * $Id: surface.c,v 3.0 89/10/27 02:06:05 craig Exp $
  1974. X *
  1975. X * $Log:    surface.c,v $
  1976. X * Revision 3.0  89/10/27  02:06:05  craig
  1977. X * Baseline for first official release.
  1978. X * 
  1979. X */
  1980. X#include <stdio.h>
  1981. X#include "constants.h"
  1982. X#include "typedefs.h"
  1983. X#include "funcdefs.h"
  1984. X
  1985. X#define blend(a, b, p, q)    (a * p + b * q)
  1986. X#define NORMCONST        (1. / 255.)
  1987. X
  1988. XSurfaceList *Surfaces;        /* Linked list of defined surfaces */
  1989. X
  1990. X/*
  1991. X * Create and return pointer to surface with given properties.
  1992. X */
  1993. XSurface *
  1994. Xmake_surface(name, amb, diff, spec, coef, refl, refr, k, translu, stcoef)
  1995. Xchar *name;
  1996. XColor *amb, *diff, *spec;
  1997. Xdouble coef, refl, refr, k, translu, stcoef;
  1998. X{
  1999. X    Surface *stmp;
  2000. X
  2001. X    /*
  2002. X     * Complain if named surface already exists.
  2003. X     */
  2004. X    if ((stmp = get_surface(name)) != (Surface *)0)
  2005. X        yyerror("Surface redefined.");
  2006. X
  2007. X    stmp = (Surface *)Malloc(sizeof(Surface));
  2008. X    stmp->name = strsave(name);
  2009. X
  2010. X    stmp->amb = *amb;
  2011. X    stmp->diff = *diff;
  2012. X    stmp->spec = *spec;
  2013. X
  2014. X    stmp->coef = coef; stmp->refl = refl; stmp->transp = refr;
  2015. X    stmp->kref = k; stmp->translucency = translu;
  2016. X    stmp->stcoef = stcoef;
  2017. X
  2018. X    return stmp;
  2019. X}
  2020. X
  2021. XSurfaceList *
  2022. Xadd_surface(surf, list)
  2023. XSurface *surf;
  2024. XSurfaceList *list;
  2025. X{
  2026. X    SurfaceList *res;
  2027. X
  2028. X    res = (SurfaceList *)Malloc(sizeof(SurfaceList));
  2029. X    res->surf = surf;
  2030. X    res->next = list;
  2031. X
  2032. X    return res;
  2033. X}
  2034. X
  2035. X/*
  2036. X * Search for surface with given name.  If not found, complain and exit.
  2037. X */
  2038. XSurface *
  2039. Xfind_surface(name)
  2040. Xchar *name;
  2041. X{
  2042. X    Surface *stmp;
  2043. X
  2044. X    stmp = get_surface(name);
  2045. X    if (stmp == (Surface *)0)
  2046. X        yyerror("Undefined surface.");
  2047. X
  2048. X    return stmp;
  2049. X}
  2050. X
  2051. X/*
  2052. X * Return pointer to surface with given name, NULL if no such surface.
  2053. X */
  2054. XSurface *
  2055. Xget_surface(name)
  2056. Xchar *name;
  2057. X{
  2058. X    SurfaceList *stmp;
  2059. X
  2060. X    for (stmp = Surfaces; stmp ; stmp = stmp->next)
  2061. X        if(strcmp(name, stmp->surf->name) == 0)
  2062. X            return stmp->surf;
  2063. X    /*
  2064. X     * No surface named "name".
  2065. X     */
  2066. X    return (Surface *)0;
  2067. X}
  2068. X
  2069. X/*
  2070. X * Compute combination of two surfaces. Resulting surface is copied into surf1.
  2071. X */
  2072. Xblend_surface(surf1, surf2, p, q)
  2073. XSurface *surf1, *surf2;
  2074. Xdouble p, q;
  2075. X{
  2076. X    /*
  2077. X      * P is weight of surf1.  q is weight of surf2.
  2078. X     * Result is placed in surf1.
  2079. X     */
  2080. X
  2081. X    blend_color(&surf1->amb, &surf2->amb, p, q);
  2082. X    blend_color(&surf1->diff, &surf2->diff, p, q);
  2083. X    blend_color(&surf1->spec, &surf2->spec, p, q);
  2084. X
  2085. X    surf1->coef = blend(surf1->coef, surf2->coef, p, q);
  2086. X    surf1->refl = blend(surf1->refl, surf2->refl, p, q);
  2087. X    surf1->transp = blend(surf1->transp, surf2->transp, p, q);
  2088. X    surf1->kref = blend(surf1->kref, surf2->kref, p, q);
  2089. X}
  2090. X
  2091. X/*
  2092. X * Blend two colors.  Result is placed in color1.
  2093. X */
  2094. Xblend_color(color1, color2, p, q)
  2095. XColor *color1, *color2;
  2096. Xdouble p, q;
  2097. X{
  2098. X    color1->r = blend(color1->r, color2->r, p, q);
  2099. X    color1->g = blend(color1->g, color2->g, p, q);
  2100. X    color1->b = blend(color1->b, color2->b, p, q);
  2101. X}
  2102. X
  2103. X/*
  2104. X * Scale color by given red, green and blue factors.
  2105. X */
  2106. XScaleColor(scalar, color, res)
  2107. Xdouble scalar;
  2108. XColor color, *res;
  2109. X{
  2110. X    res->r = color.r * scalar;
  2111. X    res->g = color.g * scalar;
  2112. X    res->b = color.b * scalar;
  2113. X}
  2114. X
  2115. XAddScaledColor(color1, scalar, color2, res)
  2116. XColor color1, color2, *res;
  2117. Xdouble scalar;
  2118. X{
  2119. X    res->r = color1.r + scalar * color2.r;
  2120. X    res->g = color1.g + scalar * color2.g;
  2121. X    res->b = color1.b + scalar * color2.b;
  2122. X}
  2123. END_OF_FILE
  2124. if test 3946 -ne `wc -c <'src/surface.c'`; then
  2125.     echo shar: \"'src/surface.c'\" unpacked with wrong size!
  2126. fi
  2127. # end of 'src/surface.c'
  2128. fi
  2129. echo shar: End of archive 2 \(of 8\).
  2130. cp /dev/null ark2isdone
  2131. MISSING=""
  2132. for I in 1 2 3 4 5 6 7 8 ; do
  2133.     if test ! -f ark${I}isdone ; then
  2134.     MISSING="${MISSING} ${I}"
  2135.     fi
  2136. done
  2137. if test "${MISSING}" = "" ; then
  2138.     echo You have unpacked all 8 archives.
  2139.     rm -f ark[1-9]isdone
  2140. else
  2141.     echo You still need to unpack the following archives:
  2142.     echo "        " ${MISSING}
  2143. fi
  2144. ##  End of shell archive.
  2145. exit 0
  2146.  
  2147.  
  2148.